package com.gkr.shopping.serviceimpl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gkr.shopping.bean.Shopcar;
import com.gkr.shopping.bean.User;
import com.gkr.shopping.mapper.ShopcarMapper;
import com.gkr.shopping.service.ShopCarService;


@Service("shopCarServiceI")
@Transactional
public class ShopCarServiceImpl implements ShopCarService {
	
	@Autowired
	private ShopcarMapper shopcarMapper;
	//将商品信息加入至购物车
	@Override
	public void addArticleToShopCar(HttpSession session, int id, int number) {
		// TODO Auto-generated method stub

		User user = (User)session.getAttribute("session_user");

		int userId = user.getId();

		Shopcar shopCar = shopcarMapper.getShopCarByUserIdAndArticleId(userId,id);
	
		if(shopCar!=null) {
			//该商品已经存在于当前用户的购车中，则进行商品数量的叠加
			shopcarMapper.updateShopCar(userId,id,number);
		}else {
			//该商品不存在与当前用户的购物车中
			shopcarMapper.addShopCar(userId,id,number);
		}	
	}

	@Override
	public List<Shopcar> getAllShopCarByUserId(HttpSession session) {
		// TODO Auto-generated method stub

		User user = (User)session.getAttribute("session_user");

		int userId = user.getId();

		List<Shopcar> shopcars = shopcarMapper.getAllShopCarByUserId(userId);
		return shopcars;
	}
	
	//更新购物车中商品的购物数量
	@Override
	public void updateShopcar(HttpSession session, int id, int number) {
		// TODO Auto-generated method stub

		User user = (User)session.getAttribute("session_user");

		int userId = user.getId();
		
		shopcarMapper.updateShopcar(userId,id,number);
		
	}

	//删除购物车中商品的购物数量
	@Override
	public void deleteShopcar(HttpSession session, int id) {
		// TODO Auto-generated method stub

        User user = (User)session.getAttribute("session_user");

		int userId = user.getId();
		
		shopcarMapper.deleteShopcar(userId,id);
	}

}
